package com.baseandroid.config;

import android.content.Context;

import com.androidbase.dagger.help.PreferencesHelper;
import com.androidbase.utils.AndroidUtils;
import com.baseandroid.gaode.MyLocation;
import com.baseandroid.retrofit.MyOkhttpHelper;
import com.orhanobut.hawk.Hawk;
import com.qianbao.qianbaobusiness.json.MerchantActivityBean;
import com.qianbao.qianbaobusiness.json.MerchantInfo;
import com.qianbao.qianbaobusiness.json.ShopListInfo;
import com.qianbao.qianbaobusiness.json.UserInfo;
import com.qianbao.qianbaobusiness.json.UserTokenInfo;

import java.util.ArrayList;
import java.util.List;

import cn.jpush.android.api.JPushInterface;

public class Global {

    public static String PREFERENCE_KEY_FIRST_USING                     = "first_using";
    public static String PREFERENCE_KEY_USER_JPUSH_ALIAS                = "jpush_alias";
    public static String PREFERENCE_KEY_USER_TOKEN_INFO                 = "user_token_info";
    public static String PREFERENCE_KEY_USER_INFO                       = "user_info";
    public static String PREFERENCE_KEY_USER_ENV                        = "user_env";
    public static String PREFERENCE_KEY_USER_LOC                        = "user_loc";
    public static String PREFERENCE_KEY_USER_PHONE                      = "user_phone";
    public static String PREFERENCE_KEY_USER_SHOPLIST                   = "user_shoplist";
    public static String PREFERENCE_KEY_USER_SELECTED_SHOPLIST          = "user_selected_shoplist";
    public static String PREFERENCE_KEY_USER_SELECTED_ANALYSIS_SHOPLIST = "user_selected_analysis_shoplist";
    public static String PREFERENCE_KEY_WECHAT_SELECTED_MERCHANT        = "user_selected_wechat_selected_merchant";
    public static String PREFERENCE_KEY_PUSH_OPEN_STATE                 = "push_open_state";
    public static String PREFERENCE_KEY_PUSH_BIll_STATE                 = "push_bill_state";
    public static String PREFERENCE_KEY_PUSH_VOICE_STATE                = "push_voice_state";
    public static String PREFERENCE_KEY_PCD_VERSION                     = "push_pcd_version";
    public static String PREFERENCE_KEY_QR_SELECTED_MERCHANT            = "user_selected_qr_selected_merchant";
    public static String PREFERENCE_KEY_PUSH_CONFIG                     = "pushconfig";
    public static String PREFERENCE_KEY_ALPHA_ZONE                      = "alphazone";
    public static String PREFERENCE_KEY_ADVERT_PATH                     = "advertpath";
    public static String PREFERENCE_KEY_FIRST_LOGIN                     = "isFirstLogin";
    public static String PREFERENCE_KEY_LOGINED_USER_PHONE              = "loginedUserPhone";

    private static MerchantActivityBean sMerchantActivityBean;

    public static boolean mbLocSuccess = false;
    private static ShopListInfo mShopListInfo;
    private static ShopListInfo mSelectedShopListInfo;
    private static ShopListInfo mSelectedAnalysisShopListInfo;

    public static Context getContext() {
        return AndroidUtils.getContext();
    }

    public static boolean getFirstUsing() {
        return PreferencesHelper.get(PREFERENCE_KEY_FIRST_USING, false);
    }

    public static void setFirstUsing(boolean firstusing) {
        PreferencesHelper.put(PREFERENCE_KEY_FIRST_USING, firstusing);
    }

    public static UserTokenInfo getUserTokenInfo() {
        return Hawk.get(PREFERENCE_KEY_USER_TOKEN_INFO, new UserTokenInfo());
    }

    public static void setUserTokenInfo(UserTokenInfo userTokenInfo) {
        Hawk.put(PREFERENCE_KEY_USER_TOKEN_INFO, userTokenInfo);
    }

    public static UserInfo getUserInfoBean() {
        return Hawk.get(PREFERENCE_KEY_USER_INFO, new UserInfo());
    }

    public static void setUserInfoBean(UserInfo userInfoBean) {
        Hawk.put(PREFERENCE_KEY_USER_INFO, userInfoBean);
    }

    public static String getsJpushAlias() {
        return PreferencesHelper.get(PREFERENCE_KEY_USER_JPUSH_ALIAS, "");
    }

    public static void setsJpushAlias(String sJpushAlias) {
        PreferencesHelper.put(PREFERENCE_KEY_USER_JPUSH_ALIAS, sJpushAlias);
    }

    public static int getUserEnv() {
        return PreferencesHelper.get(PREFERENCE_KEY_USER_ENV, 0);
    }

    public static void setUserEnv(int env) {
        PreferencesHelper.put(PREFERENCE_KEY_USER_ENV, env);
    }

    public static String getUsrePhotoNumber() {
        return PreferencesHelper.get(PREFERENCE_KEY_USER_PHONE, "");
    }

    public static void setUsrePhotoNumber(String phone) {
        PreferencesHelper.put(PREFERENCE_KEY_USER_PHONE, phone);
    }

    public static MyLocation getMyLocation() {
        return Hawk.get(PREFERENCE_KEY_USER_LOC, new MyLocation());
    }

    public static void setMyLocation(MyLocation myLocation) {
        Hawk.put(PREFERENCE_KEY_USER_LOC, myLocation);
    }

    public static void setShopListInfo(ShopListInfo shopListInfo) {
        mShopListInfo = shopListInfo;
        Hawk.put(PREFERENCE_KEY_USER_SHOPLIST, shopListInfo);
    }

    public static ShopListInfo getShopListInfo() {
        if (mShopListInfo == null) {
            mShopListInfo = Hawk.get(PREFERENCE_KEY_USER_SHOPLIST, new ShopListInfo());
        }
        return mShopListInfo;
    }

    public static void setSelectedShopListInfo(ShopListInfo shopListInfo) {
        mSelectedShopListInfo = shopListInfo;
        Hawk.put(PREFERENCE_KEY_USER_SELECTED_SHOPLIST, shopListInfo);
    }

    public static ShopListInfo getSelectedShopListInfo() {
        if (mSelectedShopListInfo == null) {
            mSelectedShopListInfo = Hawk.get(PREFERENCE_KEY_USER_SELECTED_SHOPLIST, new ShopListInfo());
        }
        return mSelectedShopListInfo;
    }

    public static void setSelectedAnalysisShopListInfo(ShopListInfo shopListInfo) {
        mSelectedAnalysisShopListInfo = shopListInfo;
        Hawk.put(PREFERENCE_KEY_USER_SELECTED_ANALYSIS_SHOPLIST, shopListInfo);
    }

    public static ShopListInfo getSelectedAnalysisShopListInfo() {
        if (mSelectedAnalysisShopListInfo == null) {
            mSelectedAnalysisShopListInfo = Hawk.get(PREFERENCE_KEY_USER_SELECTED_ANALYSIS_SHOPLIST, new ShopListInfo());
        }
        return mSelectedAnalysisShopListInfo;
    }

    public static String getWechatAppSelectedMerchant() {
        return Hawk.get(PREFERENCE_KEY_WECHAT_SELECTED_MERCHANT, "");
    }

    public static void setWechatAppSelectedMerchant(String merchantNo) {
        Hawk.put(PREFERENCE_KEY_WECHAT_SELECTED_MERCHANT, merchantNo);
    }

    public static List<MerchantInfo> getWechatAppInfos() {
        ShopListInfo shopListInfo = getShopListInfo();
        List<MerchantInfo> list = new ArrayList<>();
        for (MerchantInfo merchantInfo : shopListInfo.merchantList) {
            if (merchantInfo.getPermissionListString().contains("M100907")) {
                list.add(merchantInfo);
            }
        }
        return list;
    }

    public static String getQrCardSelectedMerchantNo() {
        return Hawk.get(PREFERENCE_KEY_QR_SELECTED_MERCHANT, "");
    }

    public static void setQrCardSelectedMerchantNo(String merchantNo) {
        Hawk.put(PREFERENCE_KEY_QR_SELECTED_MERCHANT, merchantNo);
    }

    public static boolean getPushOpenedState() {
        return PreferencesHelper.get(PREFERENCE_KEY_PUSH_OPEN_STATE, true);
    }

    public static void setPushOpenedState(boolean openstate) {
        PreferencesHelper.put(PREFERENCE_KEY_PUSH_OPEN_STATE, openstate);
    }

    public static boolean getPushBillState() {
        return PreferencesHelper.get(PREFERENCE_KEY_PUSH_BIll_STATE, true);
    }

    public static void setPushBillState(boolean openstate) {
        PreferencesHelper.put(PREFERENCE_KEY_PUSH_BIll_STATE, openstate);
    }

    public static boolean getPushVoiceState() {
        return PreferencesHelper.get(PREFERENCE_KEY_PUSH_VOICE_STATE, true);
    }

    public static void setPushVoiceState(boolean openstate) {
        PreferencesHelper.put(PREFERENCE_KEY_PUSH_VOICE_STATE, openstate);
    }

    public static MerchantActivityBean getMerchantActivityBean() {
        return sMerchantActivityBean == null ? new MerchantActivityBean() : sMerchantActivityBean;
    }

    public static void setMerchantActivityBean(MerchantActivityBean merchantActivityBean) {
        sMerchantActivityBean = merchantActivityBean;
    }

    public static void setPcdVersion(String version) {
        PreferencesHelper.put(PREFERENCE_KEY_PCD_VERSION, version);
    }

    public static String getPcdVersion() {
        return PreferencesHelper.get(PREFERENCE_KEY_PCD_VERSION, "");
    }

    public static void setPushConfig(String jpushConfig) {
        PreferencesHelper.put(PREFERENCE_KEY_PUSH_CONFIG, jpushConfig);
    }

    public static String getPushConfig() {
        return PreferencesHelper.get(PREFERENCE_KEY_PUSH_CONFIG, "");
    }

    public static void setAlphaZone(String alphaZone) {
        PreferencesHelper.put(PREFERENCE_KEY_ALPHA_ZONE, alphaZone);
    }

    public static String getAlphaZone() {
        return PreferencesHelper.get(PREFERENCE_KEY_ALPHA_ZONE, "");
    }

    public static void setAdvertFilePath(String advertFilePath) {
        PreferencesHelper.put(PREFERENCE_KEY_ADVERT_PATH, advertFilePath);
    }

    public static String getAdvertFilePath() {
        return PreferencesHelper.get(PREFERENCE_KEY_ADVERT_PATH, "");
    }

    public static void loginOutclear() {
        MyOkhttpHelper.clearCookie();
        setUserTokenInfo(new UserTokenInfo());
        setsJpushAlias("");
        setShopListInfo(new ShopListInfo());
        setSelectedShopListInfo(new ShopListInfo());
        setSelectedAnalysisShopListInfo(new ShopListInfo());
        setWechatAppSelectedMerchant("");
        setQrCardSelectedMerchantNo("");
        JPushInterface.stopPush(Global.getContext());
    }

    public static void setLoginCurrently(boolean b) {
        Hawk.put(PREFERENCE_KEY_FIRST_LOGIN, b);
    }

    public static boolean getLoginCurrently() {
        return Hawk.get(PREFERENCE_KEY_FIRST_LOGIN, true);
    }

    public static String getMerchantMobileForMerchantNo(String merchantNo) {
        for (int i = 0; i < getShopListInfo().merchantList.size(); i++) {
            if (getShopListInfo().merchantList.get(i).merchantNo.equals(merchantNo)) {
                return getShopListInfo().merchantList.get(i).managerMobile;
            }
        }
        return "";
    }

    public static void setLoginedUserPhone(String phone) {
        String loginedUserPhone = getLoginedUserPhone();
        if (loginedUserPhone.contains(phone)) {
            return;
        } else {
            Hawk.put(PREFERENCE_KEY_LOGINED_USER_PHONE, phone + "," + loginedUserPhone);
        }
    }

    public static String getLoginedUserPhone() {
        return Hawk.get(PREFERENCE_KEY_LOGINED_USER_PHONE, "");
    }
}
